package com.application.outdoor.pojo;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import ReadWriteXML.XMLBuilder;
public class LogThaoTacPOJO {
	private int id;
	private String nguoiDung;
	private String hanhDong;
	private String ghiChu;
	private long thoiGian;
	private int idCay;
	private int loai = -1;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNguoiDung() {
		return nguoiDung;
	}

	public void setNguoiDung(String nguoiDung) {
		this.nguoiDung = nguoiDung;
	}

	public String getHanhDong() {
		return hanhDong;
	}

	public void setHanhDong(String hanhDong) {
		this.hanhDong = hanhDong;
	}

	public String getGhiChu() {
		return ghiChu;
	}

	public void setGhiChu(String ghiChu) {
		this.ghiChu = ghiChu;
	}

	public long getThoiGian() {
		return thoiGian;
	}

	public void setThoiGian(long thoiGian) {
		this.thoiGian = thoiGian;
	}

	public int getIdCay() {
		return idCay;
	}

	public void setIdCay(int idCay) {
		this.idCay = idCay;
	}

	public int getLoai() {
		return loai;
	}

	public void setLoai(int loai) {
		this.loai = loai;
	}

	public LogThaoTacPOJO() {

	}

	public LogThaoTacPOJO(String nguoiDung, String ghiChu, int idCay, int loai) {
		this.nguoiDung = nguoiDung;
		this.hanhDong = "";
		this.ghiChu = ghiChu;
		this.idCay = idCay;
		this.loai=loai;
	}

	public Element createElement(Document doc) {
		Element e = doc.createElement("Log");
		e.setAttribute("ID", id + "");
		e.setAttribute("HanhDong", hanhDong);
		e.setAttribute("GhiChu", ghiChu);
		e.setAttribute("ThoiGian", thoiGian + "");
		e.setAttribute("IDCay", idCay + "");
		e.setAttribute("Loai", loai + "");
		e.setAttribute("NguoiDung", nguoiDung);
		return e;
	}

	public static String createXML(ArrayList<LogThaoTacPOJO> lstLog) {
		Document doc = XMLBuilder.BuildXML("Root");
		for (LogThaoTacPOJO log : lstLog) {
			doc.getFirstChild().appendChild(log.createElement(doc));
		}
		return XMLBuilder.docToString(doc);
		
	}

	public void parseElement(Element e) {
		this.id = Integer.parseInt(e.getAttribute("ID"));
		this.hanhDong = e.getAttribute("HanhDong");
		this.ghiChu = e.getAttribute("GhiChu");
		this.thoiGian = Long.parseLong(e.getAttribute("ThoiGian"));
		this.idCay = Integer.parseInt(e.getAttribute("IDCay"));
		this.loai = Integer.parseInt(e.getAttribute("Loai"));
		this.nguoiDung=e.getAttribute("NguoiDung");
	}

	public static ArrayList<LogThaoTacPOJO> parseList(String xml) {
		ArrayList<LogThaoTacPOJO> rs = new ArrayList<LogThaoTacPOJO>();
		try {
			Document doc = XMLBuilder.loadXMLFromString(xml);
			NodeList lstNode = doc.getElementsByTagName("Log");

			for (int i = 0; i < lstNode.getLength(); i++) {
				LogThaoTacPOJO log = new LogThaoTacPOJO();
				log.parseElement((Element) lstNode.item(i));
				rs.add(log);
			}
		} catch (Exception e) {
			rs=null;
		}
		return rs;
	}

}
